package com.tozed.android.launcher.simpleui;

import java.util.ArrayList;

import com.tozed.android.launcher.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;
import android.graphics.PorterDuff.Mode;

public class SimpleFolderPreView extends LinearLayout
{
	public static final String TAG = "SimpleFolderPreView";
	private static final boolean DEBUG = false;
	private static final int NUM_ITEMS_IN_PREVIEW = 3; // 预览图标
	private static final float PERSPECTIVE_SCALE_FACTOR = 0.35F;
	private static final float PERSPECTIVE_SHIFT_FACTOR = 0.24F;
	private ArrayList<SimpleApplicationInfo> mContentApps;
	private PreviewItemDrawingParams mParams = new PreviewItemDrawingParams(0.0F, 0.0F, 0.0F, 0);
	public static Drawable sSharedFolderLeaveBehind = null;
	private int mAvailableSpaceInPreview;
	private float mBaselineIconScale;
	private int mBaselineIconSize;
	private int mDefaultWidth = -1;
	private int mIntrinsicIconSize;
	private float mMaxPerspectiveShift;
	static int sPreviewPadding = -1;
	static int sPreviewSize = -1;
	
	private int mPreviewOffsetX;
	private int mPreviewOffsetY;
	private int mTotalWidth = -1;
	  
	
	public SimpleFolderPreView(Context context){
		this(context, null);
	}

	public SimpleFolderPreView(Context context, AttributeSet attrs){
		this(context, attrs, 0);
	}

	public SimpleFolderPreView(Context context, AttributeSet attrs, int defStyle){
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context)
	{
		mContext = context;
	    Resources res = context.getResources();
	    sPreviewSize = res.getDimensionPixelSize(R.dimen.simple_folder_preview_size);
	    sPreviewPadding = res.getDimensionPixelSize(R.dimen.simple_folder_preview_padding);
	    mDefaultWidth = res.getDimensionPixelSize(R.dimen.simple_folder_preview_width);
	}

	public void setContent(ArrayList<SimpleApplicationInfo> contentApps)
	{
		mContentApps = contentApps;
		computePreviewDrawingParams(SimpleUtilities.getIconWidth(mContext), mDefaultWidth);
		invalidate();
	}
	
	@Override
	protected void dispatchDraw(Canvas canvas)
	{
	    super.dispatchDraw(canvas);
	    
	    if (mContentApps == null)
	       return;
	    
	    int appsSize = mContentApps.size();
	    int index = NUM_ITEMS_IN_PREVIEW;
	    
	    if (index > appsSize)
	    	index = appsSize;
	    
	    // 取ContentApps集合的前三项应用图标并画出
    	index -= 1;
    	while (index >= 0)
 	    {
 	      // 计算并生成一个待画项
 		  mParams = computePreviewItemDrawingParams(index, mParams);
 		  
 		  // 取一项应用的图标
 	      mParams.bitmap = ((SimpleApplicationInfo)mContentApps.get(index)).mIcon;
 	      mParams.drawable = new BitmapDrawable(mParams.bitmap);
 	      
 	      // 画在画布上
 	      drawPreviewItem(canvas, mParams);
 	      
 	      index--;
 	    }
	}
	
	private void drawPreviewItem(Canvas paramCanvas, PreviewItemDrawingParams previewItem)
	{
		paramCanvas.save();
		
		paramCanvas.translate(previewItem.transX + mPreviewOffsetX, previewItem.transY + mPreviewOffsetY);
		paramCanvas.scale(previewItem.scale, previewItem.scale);
		
		Drawable drawable = previewItem.drawable;
		
		if (drawable != null)
		{
			if (DEBUG){
				Log.i(TAG, "SimpleFolderPreView d = " + drawable+ "params.scale = " + previewItem.scale
						+ " params.transX = " + previewItem.transX + "mPreviewOffsetX "+ mPreviewOffsetX 
						+ "params.transY = " + previewItem.transY  + "mPreviewOffsetY = " + this.mPreviewOffsetY);
				
				Log.i(TAG, "SimpleFolderPreView d.getIntrinsicWidth() = " + drawable.getIntrinsicWidth()
								+ " = d.getIntrinsicHeight()" + drawable.getIntrinsicHeight());
			}
			
			drawable.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
			drawable.setFilterBitmap(true);
			drawable.setColorFilter(Color.argb(previewItem.overlayAlpha, 0, 0, 0), Mode.SRC_ATOP); // 关键参数
			drawable.draw(paramCanvas);
			drawable.clearColorFilter();
			drawable.setFilterBitmap(false);
		}
		
		paramCanvas.restore();
	}
	
	// 计算预览项参数
	private void computePreviewDrawingParams(int iconWidth, int totalWidth)
	{
		Log.i(TAG, "computePreviewDrawingParams drawableSize = " + iconWidth + " drawableSize = " 
				+ iconWidth + " totalSize=" + " "
				+ totalWidth + "sPreviewSize = " + sPreviewSize
				+ " sPreviewPadding = " + sPreviewPadding);
		
		if (mIntrinsicIconSize == iconWidth && mTotalWidth == totalWidth)
			return;
		
		mIntrinsicIconSize = iconWidth;
		mTotalWidth = totalWidth;
		mAvailableSpaceInPreview = (sPreviewSize - sPreviewPadding * 2);
		
		int space = (int) (1.8F * (mAvailableSpaceInPreview / 2.0F));
		int iconScale = (int) (1.24F * mIntrinsicIconSize);
		
		mBaselineIconScale = (1.0F * space / iconScale);
		mBaselineIconSize = (int) (mIntrinsicIconSize * mBaselineIconScale);
		mMaxPerspectiveShift = (0.24F * mBaselineIconSize);
		mPreviewOffsetX = ((mTotalWidth - mAvailableSpaceInPreview) / 2);
		mPreviewOffsetY = sPreviewPadding;
	}
	
	private PreviewItemDrawingParams computePreviewItemDrawingParams(int index, PreviewItemDrawingParams previewItem)
	{
		float f1 = 1.0F * ((3 - index) - 1) / 2.0F;
		float f2 = 1.0F - 0.35F * (1.0F - f1);
		float f3 = (1.0F - f1) * mMaxPerspectiveShift;
		float f4 = f2 * mBaselineIconSize;
		float f5 = (1.0F - f2) * mBaselineIconSize;
		
		float transY = mAvailableSpaceInPreview - (f5 + (f3 + f4));
		float transX = f3 + f5;
		float scale = f2 * mBaselineIconScale;
		int overlayAlpha = (int) (80.0F * (1.0F - f1));
		
		if (previewItem == null)
			return new PreviewItemDrawingParams(transX, transY, scale, overlayAlpha);
		
		previewItem.transX = transX;
		previewItem.transY = transY;
		previewItem.scale = scale;
		previewItem.overlayAlpha = overlayAlpha;
		
		return previewItem;
	}
	
	// 预览数据项
	static class PreviewItemDrawingParams
	{
		Bitmap bitmap;
		Drawable drawable;
		int overlayAlpha;
		float scale;
		float transX;
		float transY;

		PreviewItemDrawingParams(float transX, float transY, float scale, int overlayAlpha){
			this.transX = transX;
			this.transY = transY;
			this.scale = scale;
			this.overlayAlpha = overlayAlpha;
		}
	}
}
